/*
 * Copyright (c) 2009, DanikX Corporation. All Rights Reserved.
 */

package kz.home.utils;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

/**
 * <strong>title</strong>
 * <p> describe
 *
 * @author: Danik <br>
 * Date: 08.07.2009  <br>
 * Time: 19:16:37  <br>
 */
public class Utils {

    public static String[] getValues(Vector vector) {
        String[] str = new String[vector.size()];
        for (int i = 0; i < vector.size(); i++) str[i] = "" + vector.elementAt(i);
        //
        return str;
    }

    public static String getCurrentDate() {
        Date date = new Date(System.currentTimeMillis());
        Calendar cal;
        (cal = Calendar.getInstance()).setTime(date);

        return cal.get(Calendar.YEAR) + "-" + getCorrectValue(cal.get(Calendar.MONTH) + 1) + "-" + getCorrectValue(cal.get(Calendar.DATE));
    }
 
    private static String getCorrectValue(int value) {
        return (value < 10) ? "0" + value : "" + value;
    }

    public static Vector getTokens(String text, String symbol) {
        Vector result = new Vector();
        int end, start = 0;

        while ((end = text.indexOf(symbol, start)) > -1) {
            result.addElement(text.substring(start, end));
            start = end + 1;
        }
        if (text.length() > end) result.addElement(text.substring(start, text.length()));
        //
        return result;
    }

    public static Vector getValues(byte[] seed, String symbol) {
        try {
            return getTokens(new String(seed, "UTF-8"), symbol);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getID(byte[] seed, String symbol) {
        try {
            Vector vv = getTokens(new String(seed, "UTF-8"), symbol);
            if (vv.size() > 0) {
                return "" + vv.elementAt(0);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Vector union(Vector vec1, Vector vec2) {
        Vector result = new Vector();

        for (int i = 0; i < vec1.size(); i++) {
            if (vec1.elementAt(i) instanceof String) {
                for (int j = 0; j < vec2.size(); j++) {
                    if (vec2.elementAt(j) instanceof String) {
                        String str = (String) vec1.elementAt(i);
                        String str2 = (String) vec2.elementAt(j);
                        if (str != null && str2 != null && str.equals(str2)) {
                            result.addElement(str2);
                        }
                    }
                }
            }
        }
        return result;
    }

    public static void printVector(Vector vector) {
        if (vector == null) {
            print("vector[null]");
            return;
        }
        if (vector.size() == 0) {
            print("vector[size == 0]");
            return;
        }

        String result = "";
        for (int i = 0; i < vector.size(); i++) {
            if (vector.elementAt(i) instanceof Vector) {
                Vector innerVector = (Vector) vector.elementAt(i);
                for (int j = 0; j < innerVector.size(); j++) {
                    result += innerVector.elementAt(j) + " ";
                }
            }

            if (vector.elementAt(i) instanceof String) {
                String str = (String) vector.elementAt(i);
                result += str + " ";
            }

            print(result);
            result = "";
        }
    }

    public static void print(String message) {
        System.out.println(message);
    }
}
